
package gblend.formatcovert;


/*
* Copyright 2010, Center for Advanced Information Systems, Nanyang Technological University
*
* File name: AIDStoQuickSI.java
*
* Abstract: Convert standard format to Ctree format
*
* Current Version:      0.1
* Auther:               Jin Changjiu
* Modified Date:        June.3,2009
*
*/
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Vector;
public class StandardtoCtree {
      
    private static String filename =null;
    public static void main (String[] args) throws FileNotFoundException
    {
         filename = "AIDS40k";
         formatDotFile(filename);
    }


    public static void formatDotFile(String filename)
    {
        String output1=filename+".txt";
        String strLine = null;
	FileOutputStream outStream1;
	PrintStream p;
        PrintStream q;
	FileInputStream inStream;
	DataInputStream in;
	BufferedReader br;

	File fin=new File(filename);
	File fout1=new File(output1);
        if(fin.exists())
	{
            try {
                    outStream1=new FileOutputStream(fout1,false);
                    inStream=new FileInputStream(fin);
                    // Connect print stream to the output stream
                    p = new PrintStream( outStream1 );
                    // Get the object of DataInputStream
                    in = new DataInputStream(inStream);
                    br = new BufferedReader(new InputStreamReader(in));
                    //Read file line by line
    		    try {

                      int vn = 0;
                      int en = 0;
                      int graphnum = -1;

                   
                      Vector<String> nodelable = null;
                      Vector<String> edge = null;
                      while ((strLine = br.readLine()) != null)
                      {
                       // Print the content on the console
                          if(strLine.contains("t #"))
                          {
                              graphnum++;
                              vn = 0;
                              en = 0;
                              nodelable = new Vector<String>();
                              edge = new Vector<String>();
                           }
                           else if(strLine.contains("v"))
                           {
                               vn++;
                               String[] strnode = strLine.split("\\s");
                               nodelable.addElement(strnode[2]);

                           }
                           else if(strLine.contains("e"))
                           {
                               en++;
                               String[] stredge = strLine.split("\\s");
                               edge.addElement(stredge[1]+" "+stredge[2]);

                           }
                           else
                           {
                              p.println("#"+graphnum);
                              p.println(vn);
                              for(int i = 0; i< vn;i++)
                              {
                                   p.println(nodelable.elementAt(i));
                              }

                              p.println(en);
                              for(int i = 0; i< edge.size();i++)
                              {
                                 String[] strnode = edge.elementAt(i).split("\\s");
                                 int e1 = Integer.parseInt(strnode[0]);
                                 int e2 = Integer.parseInt(strnode[1]);
                                 p.println(e1+" "+e2);
                              }
                           }
                    }
                    //Close the input stream
                    in.close();
                    p.close();

		}
                catch (IOException e)
                {
                // TODO Auto-generated catch block
                    e.printStackTrace();
		}
           } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
                e.printStackTrace();
            }
	}

    }


}
